//
//  GFResturtantDetialTableViewCell.m
//  GoodFood
//
//  Created by dllo on 15/10/30.
//  Copyright © 2015年 荣伟峰. All rights reserved.
//

#import "GFResturtantDetialTableViewCell.h"


@interface GFResturtantDetialTableViewCell ()
@property (nonatomic, strong) UILabel *titleLabel;
@property (nonatomic, strong) UIImageView *starImageView;
@property (nonatomic, strong) UIImageView *timeImageView;
@property (nonatomic, strong) UILabel *openTimeLabel;
@property (nonatomic, strong) UIImageView *likeImageView;
@property (nonatomic, strong) UILabel *likeLabel;
@property (nonatomic, strong) UIImageView *lookImageView;
@property (nonatomic, strong) UILabel *lookLabel;
@end

@implementation GFResturtantDetialTableViewCell
#pragma mark - 初始化
- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier {
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        [self creatLabel];
        [self creatImageView];
    }
    return self;
}
#pragma mark - 布局
- (void)layoutSubviews {
    [super layoutSubviews];
    self.titleLabel.frame = CGRectMake(5, 5, ScreenWidth - 5, ScreenHeight / 20);
    
    
    for (int i = 0; i < 5; i ++) {
        UIImageView *imageView = (UIImageView *)[self.contentView viewWithTag:200 + i];
        imageView.frame = CGRectMake(5 + (ScreenHeight / 30) * i, 5 + ScreenHeight / 20, ScreenHeight / 30, ScreenHeight / 30);
        
    }

    if (!(self.model.open_time == nil) && !([self.model.open_time isEqualToString:@""]) ) {

        self.timeImageView.frame = CGRectMake(5, ScreenHeight / 30 +ScreenHeight / 20 + 8, ScreenHeight / 40, ScreenHeight / 40);
        self.openTimeLabel.frame = CGRectMake(5 + ScreenHeight / 40, 5 +ScreenHeight / 20 + ScreenHeight / 30 , ScreenWidth - 5 - ScreenWidth / 40, ScreenHeight / 30);

    } else {
        self.timeImageView.frame = CGRectMake(0, 0, 0, 0);
        self.openTimeLabel.frame = CGRectMake(0, 0, 0, 0);

    }
    if (!(0 == self.model.favorites_total)) {
        CGRect rectForLike = [[NSString stringWithFormat:@"被%ld人收藏", self.model.favorites_total] boundingRectWithSize:CGSizeMake(ScreenWidth / 5, 0) options:1 attributes:@{NSFontAttributeName:[UIFont systemFontOfSize:12]} context:nil];
        self.likeImageView.frame = CGRectMake(5,8 +ScreenHeight / 20 + ScreenHeight / 30 + self.openTimeLabel.frame.size.height , ScreenHeight / 40, ScreenHeight / 40);
        self.likeLabel.frame = CGRectMake(5 + ScreenHeight / 40, 5 +ScreenHeight / 20 + ScreenHeight / 30 + self.openTimeLabel.frame.size.height, rectForLike.size.width, ScreenHeight / 30);
        
    } else {
        self.likeImageView.frame = CGRectMake(0, 0, 0, 0);
        self.likeLabel.frame = CGRectMake(0, 0, 0, 0);

    }
    
    
    if (!(0 == self.model.view_total)) {
        self.lookImageView.frame = CGRectMake(5 + self.likeLabel.frame.origin.x + self.likeLabel.frame.size.width, 8 +ScreenHeight / 20 + ScreenHeight / 30 + self.openTimeLabel.frame.size.height, ScreenHeight/ 40, ScreenHeight / 40);
        self.lookLabel.frame = CGRectMake(5 + self.likeLabel.frame.origin.x + self.likeLabel.frame.size.width + ScreenHeight / 40, 5 +ScreenHeight / 20 + ScreenHeight / 30 + self.openTimeLabel.frame.size.height, ScreenWidth - (5 + self.likeLabel.frame.origin.x + self.likeLabel.frame.size.width + ScreenHeight / 40), ScreenHeight / 30);
    } else {
        self.lookImageView.frame = CGRectMake(0, 0, 0, 0);
        self.lookLabel.frame = CGRectMake(0, 0, 0, 0);
    }
    

    
}
#pragma mark - set
- (void)setModel:(GFResturtantDetialModel *)model {
    if (_model != model) {
        _model = model;
    }
    //标题
    self.titleLabel.text = model.place_name;
    //评分星级
    NSString *star = [NSString stringWithFormat:@"%.1f", model.star];
    NSRange rangeForFloat = {2,1};
    NSRange rangeForInt = {0,1};
    for (int i = 0; i < 5; i ++) {
        UIImageView *imageView = (UIImageView *)[self.contentView viewWithTag:200 + i];
        imageView.image = [UIImage imageNamed:@"wuxing.png"];
        NSString *strTail = [star substringWithRange:rangeForFloat];
        NSString *strHead = [star substringWithRange:rangeForInt];
        if ([strTail isEqualToString:@"0"]) {
            for (int j = 0; j < [strHead integerValue]; j ++) {
                UIImageView *imageView2 = (UIImageView *)[self.contentView viewWithTag:200 + j];
                imageView2.image = [UIImage imageNamed:@"quanxing.png"];
            }
        } else {
            for (int j = 0; j < [strHead integerValue]; j ++) {
                UIImageView *imageView2 = (UIImageView *)[self.contentView viewWithTag:200 + j];
                imageView2.image = [UIImage imageNamed:@"quanxing.png"];
            }
            UIImageView *imageView3 = (UIImageView *)[self.contentView viewWithTag:200 + [strHead integerValue]];
            imageView3.image = [UIImage imageNamed:@"banxing.png"];
        }
        
    }

    //营业时间图标
    self.timeImageView.image = [UIImage imageNamed:@"iconfont-llstoredetailtime"];
    //营业时间
    self.openTimeLabel.text = [NSString stringWithFormat:@"营业时间:%@", model.open_time];
    //收藏图标
    self.likeImageView.image = [UIImage imageNamed:@"iconfont-xihuan-2"];
    //收藏
    self.likeLabel.text = [NSString stringWithFormat:@"被%ld人收藏", model.favorites_total];
    //浏览图标
    self.lookImageView.image = [UIImage imageNamed:@"iconfont-liulan"];
    //浏览
    self.lookLabel.text = [NSString stringWithFormat:@"浏览%ld次", model.view_total];
}
#pragma mark - 创建label
- (void)creatLabel {
    self.titleLabel = [[UILabel alloc] init];
    self.titleLabel.font = [UIFont systemFontOfSize:15];
    [self.contentView addSubview:self.titleLabel];
    
    self.openTimeLabel = [[UILabel alloc] init];
    self.openTimeLabel.font = [UIFont systemFontOfSize:12];
    [self.contentView addSubview:self.openTimeLabel];
    
    self.likeLabel = [[UILabel alloc] init];
    self.likeLabel.font = [UIFont systemFontOfSize:12];
    [self.contentView addSubview:self.likeLabel];
    
    self.lookLabel = [[UILabel alloc] init];
    self.lookLabel.font = [UIFont systemFontOfSize:12];
    [self.contentView addSubview:self.lookLabel];
    

    
    
}
#pragma mark - 创建imageView
- (void)creatImageView {
    for (int i = 0; i < 5; i ++) {
        self.starImageView = [[UIImageView alloc] init];
        self.starImageView.tag = 200 + i;
        [self.contentView addSubview:self.starImageView];
    }
    
    self.timeImageView =[[UIImageView alloc] init];
    [self.contentView addSubview:self.timeImageView];
    
    self.likeImageView = [[UIImageView alloc] init];
    [self.contentView addSubview:self.likeImageView];
    
    self.lookImageView = [[UIImageView alloc] init];
    [self.contentView addSubview:self.lookImageView];

}
#pragma mark - 自适应高度
+ (CGFloat)heightForCellWithTime:(NSString *)openTime withLike:(NSInteger)like withLook:(NSInteger)look {
    CGFloat height = 0;
    if (!(openTime == nil) && !([openTime isEqualToString:@""])) {
        height = height + ScreenHeight/ 30;
    }
    if (!(0 == like)) {
        height = height + ScreenHeight / 30;
    } else {
        if (!(0 == look)) {
            height = height + ScreenHeight / 30;
        }
    }
    
    return 10 +ScreenHeight / 20 + ScreenHeight / 30 + height;
}
- (void)awakeFromNib {
    // Initialization code
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}

@end
